/*
 * Copyright (c) 2020-2099 the original author or authors. All rights reserve.
 */
package org.tianyun.cloud.security.server.spring;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import org.tianyun.cloud.security.configuration.SecurityProperties;
import org.tianyun.cloud.security.server.SecurityFilterChainBuilder;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * 安全过滤器，GatewaySecurityFilter的代理
 *
 * @auther ebert_chan
 */
public class GatewaySecurityFilterProxy implements GatewayFilter, Ordered {

    /**
     * 高优先级Filter，通常系统框架使用
     */
    public static final int HIGHEST_PRECEDENCE_FILTER = 0;

    private SecurityProperties securityProperties;

    private List<GatewaySecurityFilter> gatewaySecurityFilters;

    public GatewaySecurityFilterProxy(SecurityProperties securityProperties, List<GatewaySecurityFilter> gatewaySecurityFilters) {
        this.securityProperties = securityProperties;
        this.setGatewaySecurityFilters(gatewaySecurityFilters);
    }

    public void setGatewaySecurityFilters(List<GatewaySecurityFilter> gatewaySecurityFilters) {
        this.gatewaySecurityFilters = gatewaySecurityFilters;
        if (!CollectionUtils.isEmpty(gatewaySecurityFilters)) {
            this.gatewaySecurityFilters.sort((o1, o2) -> {
                return o1.getOrder() == o2.getOrder() ? 0 : (o1.getOrder() > o2.getOrder() ? 1 : -1);
            });
        }
    }

    /*
     * @see org.springframework.cloud.gateway.filter.GatewayFilter#filter(org.springframework.web.server.ServerWebExchange,
     * org.springframework.cloud.gateway.filter.GatewayFilterChain)
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        if (CollectionUtils.isEmpty(gatewaySecurityFilters) || !securityProperties.isEnabled()) {
            return chain.filter(exchange);
        }

        SecurityFilterChainBuilder<ServerHttpRequest, ServerHttpResponse> builder = SecurityFilterChainBuilder.gateway();

        for (GatewaySecurityFilter gatewaySecurityFilter : gatewaySecurityFilters) {
            builder.addSecurityFilter(gatewaySecurityFilter);
        }

        GatewaySecurityWebExchange webExchange = new SimpleGatewaySecurityWebExchange(exchange);
        Mono<Void> mono = builder.build().doFilter(webExchange);
        return mono == null ? Mono.empty() : chain.filter(exchange);
    }

    /*
     * @see org.springframework.core.Ordered#getOrder()
     */
    @Override
    public int getOrder() {
        return HIGHEST_PRECEDENCE_FILTER + 10;
    }

}
